﻿using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using mal.balades.SlideShow.Message;

namespace mal.balades.SlideShow.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    [ScriptableType]
    public class MainViewModel : ViewModelBase
    {

        #region Fields
        private PlayMode mode = PlayMode.Pause;
        private ObservableCollection<ImageViewModel> pictures;

        private string playToolTip = "Démarrer le diaporama.";

        private string pauseToolTip = "Arrêter le diaporama.";

        private RelayCommand playCommand;
        private RelayCommand closeCommand; 
        
        #endregion

        #region Construction / Destruction
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
                AddPicture("Vue sur la campagne du Luberon", "http://autourdeaix.free.fr/photos/Ansouis/ansouis-011.jpg");
            }
            else
            {
                HtmlPage.RegisterScriptableObject("MainViewModel", this);
            }
        }
        #endregion

        #region enum, struct, class
        public enum PlayMode
        {
            Play,
            Pause
        }
        #endregion

        #region Properties
        private PlayMode Mode
        {
            get { return mode; }
            set
            {
                if (mode != value)
                {
                    mode = value;
                     RaisePropertyChanged("PlayToolTip");
                    RaisePropertyChanged("IsPlaying");
                    
                    //Messenger.Default.Send<PlayMessage>(new PlayMessage(mode));
                }
            }
        }

        public ObservableCollection<ImageViewModel> Pictures
        {
            get { return pictures; }
            set
            {
                if (pictures != value)
                {
                    pictures = value;
                    RaisePropertyChanged("Pictures");
                }
            }
        }

        public bool IsPlaying
        {
            get
            {
                switch (Mode)
                {
                    case PlayMode.Play:
                        return true;
                    case PlayMode.Pause:
                        return false;
                }
                return false;
            }
        }     
        
        public string PlayToolTip
        {
            get
            {
                switch (Mode)
                {
                    case PlayMode.Play:
                        return pauseToolTip;
                    case PlayMode.Pause:
                        return playToolTip;
                }
                return string.Empty;
            }  
        }

        public ICommand PlayCommand
        {
            get
            {
                if (this.playCommand == null)
                {
                    this.playCommand = new RelayCommand(OnPlay);
                }
                return this.playCommand;
            }
        }

        public ICommand CloseCommand
        {
            get
            {
                if (this.closeCommand == null)
                {
                    this.closeCommand = new RelayCommand(OnClose, CanClose);
                }
                return this.closeCommand;
            }
        }
        #endregion

        #region Methods
        [ScriptableMember]
        public void AddPicture(string title, string url)
        {
            if (this.Pictures == null)
                this.Pictures = new ObservableCollection<ImageViewModel>();

            this.Pictures.Add(new ImageViewModel() { ImageUrl = url, Title = title });
        }

        [ScriptableMember]
        public void ShowPicture(string url)
        {
            ImageViewModel selectedPicture = null;
            foreach (ImageViewModel picture in this.Pictures)
            {
                if (picture.ImageUrl == url)
                    selectedPicture = picture;
            }

            if (selectedPicture != null)
            {
                this.Pictures.Remove(selectedPicture);
                this.Pictures.Insert(0, selectedPicture);
            }
        }


        protected void OnPlay()
        {
            switch (mode)
            {
                case PlayMode.Play:
                    Mode = PlayMode.Pause;
                    break;
                case PlayMode.Pause:
                    Mode = PlayMode.Play;
                    break;
                default:
                    break;
            }
        }


        protected void OnClose()
        {
            HtmlPage.Window.Invoke("CloseSlideShow", null);
        }

        protected bool CanClose()
        {
            return true;
        }
        #endregion

        #region Events

        #endregion


    }



}